package top.yamhk.agent.entity.dto;

import cn.hutool.core.util.NumberUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import top.yamhk.agent.entity.enums.MsgType;
import top.yamhk.agent.entity.response.BaseMessage;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * 消息实体类
 *
 * @author 庆 HongKong
 * @date 2019/5/23 22:22
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class Message extends BaseMessage implements Serializable {
    /**
     * 消息ID，用于轮询功能
     */
    private Long id;
    /**
     * 接收者id
     * 可根据toUserId是否为null判断是群发还是单发
     */
    private String toUserId;
    /**
     * 用户编号
     */
    private String frUserId;
    private String frNickname;
    /**
     * 消息内容
     */
    private String contentText;
    /**
     * 用户令牌
     */
    private String accessToken;
    /**
     * 消息类型
     */
    private Integer itemType;
    /**
     * 消息类型
     */
    private String itemDesc;
    /**
     * 消息主题（统一参数名）
     */
    private String topic;
    /**
     * 消息类型
     */
    private String parentId;


    /**
     * 发给谁
     */
    private String addressee;
    /**
     * 发送人
     */
    private String msgFrom;
    /**
     * 消息频道
     */
    private String msgGroup;
    /**
     * 消息字符集
     */
    private String charset;
    /**
     * 渲染
     */
    private String rendering;
    /**
     * 消息内容
     */
    private String content;
    /**
     * 用户省份
     */
    private String province;
    /**
     * 公网IP
     */
    private String publicIp;

    public static boolean isAnOrder(String message) {
        return message.startsWith("/");
    }

    public static boolean isMonitorOrder(String message) {
        return message.startsWith("?");
    }

    public static boolean isSubscribe(String message) {
        return message.startsWith("#");
    }

    public static boolean isMessage(String message) {
        String[] split = message.split(" ");
        return split.length == 2 && NumberUtil.isNumber(split[0]);
    }

    public void beautySelf() {
        List<String> msgGroup = Arrays.asList(this.msgFrom, this.addressee);
        if (this.itemType.equals(MsgType.SINGLE2SINGLE.getCode())) {
            msgGroup.sort(String::compareTo);
            this.msgGroup = msgGroup.toString();
        }
    }

    public boolean isSingle() {
        return String.valueOf(MsgType.SINGLE2SINGLE.getCode()).equals(this.msgGroup);
    }

    public boolean isHeartBeat() {
        return MsgType.HEART_BEAT_01.getCode().equals(this.getItemType());
    }

    public boolean isHandsome() {
        return MsgType.SYN_SENT.getCode().equals(this.getItemType()) || MsgType.SYN_RECV.getCode().equals(this.getItemType()) || MsgType.ESTABLISHED.getCode().equals(this.getItemType());
    }

    public boolean isHeartBeatFromServer() {
        return MsgType.HEART_BEAT_01.getCode().equals(this.getItemType()) && "CoreServer4".equals(this.getMsgFrom());
    }
    
    // 为轮询版本添加的getter和setter方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public String getTopic() {
        return topic;
    }
    
    public void setTopic(String topic) {
        this.topic = topic;
    }
}